package cn.iocoder.yudao.module.scrm.dal.mysql.clue;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.scrm.controller.admin.clue.vo.CluePageReqVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.clue.ClueDO;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.util.StringUtils;

/**
 * 线索 Mapper
 *
 * @author admin
 */
@Mapper
public interface ClueMapper extends BaseMapperX<ClueDO> {

    default PageResult<ClueDO> selectPage(CluePageReqVO reqVO) {
        LambdaQueryWrapperX<ClueDO> queryWrapper = new LambdaQueryWrapperX<>();
        // 处理关键词查询
        if (StringUtils.hasText(reqVO.getKey())) {
            queryWrapper.and(query -> query
                    .like(ClueDO::getName, reqVO.getKey())
                    .or().like(ClueDO::getEnterprise, reqVO.getKey())
                    .or().like(ClueDO::getPhone, reqVO.getKey()));
        }
        queryWrapper.eq(StringUtils.hasText(reqVO.getClientQuality()),ClueDO::getClientQuality, reqVO.getClientQuality())
                .in(reqVO.getClueState()!=null && !reqVO.getClueState().isEmpty(),ClueDO::getClueState, reqVO.getClueState())
                .between(reqVO.getCreateTime()!=null && reqVO.getCreateTimeEnd()!=null,ClueDO::getAcquisitionTime,reqVO.getCreateTime(),reqVO.getCreateTimeEnd())
                .eq(StringUtils.hasText(reqVO.getClueSource()),ClueDO::getClueSource, reqVO.getClueSource())
                .eq(StringUtils.hasText(reqVO.getClientSource()),ClueDO::getClientSource, reqVO.getClientSource())


                .eq(reqVO.getAcquisitionTime() != null ,ClueDO::getAcquisitionTime , reqVO.getAcquisitionTime())
                .eq(StringUtils.hasText(reqVO.getMaterialSource()),ClueDO::getMaterialSource,reqVO.getMaterialSource())
                .eq(StringUtils.hasText(reqVO.getInfoSource()),ClueDO::getInfoSource,reqVO.getInfoSource())
                .like(StringUtils.hasText(reqVO.getRemark()), ClueDO::getRemark, reqVO.getRemark())


                .in(reqVO.getUserId() != null && !reqVO.getUserId().isEmpty(), ClueDO::getUserId, reqVO.getUserId())
                .eq(StringUtils.hasText(reqVO.getDeptId()),ClueDO::getDeptId,reqVO.getDeptId())

                .orderByDesc(ClueDO::getCreateTime);
        return selectPage(reqVO, queryWrapper);
    }

}